import 'package:common/base/base_lv_adapter.dart';
import 'package:common/base/base_multi_lv_state.dart';
import 'package:common/base/base_stateful_widget.dart';
import 'package:common/constant/icon_data_constant.dart';
import 'package:flutter/material.dart';

import '../model/multi_select_list_bean.dart';
import '../present/home_present.dart';

class MultiSelectPage extends BaseStatefulWidget {
  const MultiSelectPage({super.key});

  @override
  State<MultiSelectPage> createState() => _MultiSelectPageState();
}

class _MultiSelectPageState
    extends BaseMultiLvState<MultiSelectPage, MultiSelectListRowBean> {

  late HomePresent _homePresent;
  @override
  void onStateInit() {
    super.onStateInit();
    _homePresent = HomePresent(this);
  }

  @override
  bool onNeedAppBar() {
    return false;
  }

  @override
  BaseLvAdapter<MultiSelectListRowBean> onInitAdapter() {
    return BaseLvAdapter<MultiSelectListRowBean>((MultiSelectListRowBean t, int index) {
      return Container(
        height: 80,
        margin: const EdgeInsets.fromLTRB(5, 10, 5, 0),
        decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(5)),
        child: InkWell(
          onLongPress: () {
            restoreSelectedMode();
          },
          child: Stack(
            alignment: AlignmentDirectional.centerStart,
            children: <Widget>[
              Wrap(
                spacing: 5,
                children: [
                  Visibility(
                      visible: t.visible,
                      child: StatefulBuilder(builder:
                          (BuildContext context, void Function(void Function()) setItemCbState) {
                        return Checkbox(
                            value: t.selected,
                            onChanged: (value) {
                              if (value == true) {
                                selectMap?.addEntries([MapEntry(index, t)]);
                              } else {
                                selectMap?.remove(index);
                              }
                              t.selected = !t.selected;
                              if (selectMap?.length == adapter.getItemCount()) {
                                allSelected.value = true;
                              } else {
                                allSelected.value = false;
                              }
                              setSelectDescRtState(() {});
                              setItemCbState(() {});
                            });
                      })),
                  const Image(
                      width: 40,
                      height: 40,
                      image: AssetImage("assets/images/xxhdpi/settings.png", package: "resource"),
                      fit: BoxFit.scaleDown),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      const Text(
                          style: TextStyle(
                            color: Colors.black,
                          ),
                          "智能识别(电子围栏)"),
                      Text(
                          style: const TextStyle(color: Colors.lightBlueAccent, fontSize: 12),
                          t.reportVideoName),
                      Text(style: const TextStyle(color: Colors.grey, fontSize: 12), t.reportTime)
                    ],
                  )
                ],
              ),
              const Align(alignment: Alignment(1, 0.8), child: Icon(IconDataConstant.arrowRight, color: Colors.black26)),
              const Positioned(
                  right: -2,
                  top: 0,
                  child: Image(
                      width: 50,
                      height: 50,
                      alignment: Alignment.topRight,
                      image: AssetImage("assets/images/xxhdpi/un_read.png", package: "resource"),
                      fit: BoxFit.scaleDown)),
            ],
          ),
        ),
      );
    });
  }

  //创建ListView的Item,并且绑定数据

  @override
  void onLoadData() {
    showDelayLoadingDialog(context);
    Future.delayed(const Duration(seconds: 3), () {
      _homePresent.getMultiSelectList();
    });
  }

  @override
  Future<void> onPullRefresh() async {
    super.onPullRefresh();
    await Future.delayed(const Duration(seconds: 3), () {});
  }

  @override
  Future<void> onPullLoadMore() async {
    super.onPullLoadMore();
    await Future.delayed(const Duration(seconds: 3), () {});
  }

  @override
  void onSuccess<D>(D d , String apiPath) {
    super.onSuccess(d, apiPath);
    if(d is MultiSelectListBean) {
      MultiSelectListBean bean = d;
      refreshOrLoadSuccess(bean.rows, total: bean.total);
    }
  }
}
